package com.guit.hrms.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class MD5Util {
    public static String encrypt(String str) {
        //将密码转为md5加密
        try {
            // 要加密的字符串
            String data = str;

            // 创建MD5对象
            MessageDigest md = MessageDigest.getInstance("MD5");

            // 更新MD5对象以包含字符串的二进制数据
            md.update(data.getBytes());

            // 计算MD5散列值
            byte[] digest = md.digest();

            // 将字节数组转换为十六进制字符串
            StringBuilder sb = new StringBuilder();
            for (byte b : digest) {
                sb.append(String.format("%02x", b & 0xff));
            }
            str = sb.toString();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
        return str;
    }
}
